<?php
class NewsAction Extends SaleAction
{
    public function index()
    {
           $M=M("News");
           $list=$M->field("`id`,`title`,`published`,`cid`,`aid`")->order("`published` DESC")->page($_GET['p'])->limit(10)->select();
           $statusArr = array("审核状态", "已发布状态");

           $aidArr = M("employee")->field("`id`,`name`")->select();
            foreach ($aidArr as $k => $v) {
                $aids[$v['id']] = $v;
            }
            unset($aidArr);//注销定义

           $cidArr = M("Category")->field("`cid`,`name`")->select();
           foreach ($cidArr as $k => $v)
           {
                $cids[$v['cid']] = $v;
           }
           unset($cidArr);

           foreach ($list as $k => $v)
           {
                $list[$k]['aidName'] =$aids[$v['aid']]['name'];
                $list[$k]['cidName'] = $cids[$v['cid']]['name'];
            }
           

           import("ORG.Util.Page");
           $totalnum=$M->count();
           $this->assign("list",$list);
           $page=new Page($totalnum,10);
           $page->rollPage=10;
           $page->setConfig("prev","上一页");
           $page->setConfig("next","下一页");
           $page->setConfig("first","首页");
           $page->setConfig("last","末页");
           $page->setConfig("theme","共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");
           $pageshow=$page->show();
           $this->assign("pageshow",$pageshow);
           $this->display();
    }

    public function ShowNews()
    {
    	$News=M('News');
    	$id=$_GET['id'];
        $news_Item=$News->where("id=$id")->find();
        $this->info=$news_Item;
    	$this->display();
    }
    
    public function add()
    {
        $this->categoryList=$this->GetData("category","cid,name");
        $this->action = "add";
        $this->display();
    }
    
    public function edit()
    {
    	$news=M('News');
        $id = $_GET['id'];
        $news_Item=$news->where("id=$id")->find();
        $this->info = $news_Item;
        $this->categoryList = $this->GetData("category","cid,name");
        $this->action="Edit";
        $this->display();
    }

    public function save()
    {
        $news = D('News');
        $data = $news->create();
        if (!$data) $this->error($news->getError());
        if($_POST['action']=="Edit")
        {
        	 //dump($data);exit;
        	 if($news->save($data)) // 根据条件保存修改的数据
             {
	             $this->success('成功提交');
		     } 
		     else
		     {
		        $this->error("提交失败");
		     }
        }
        else 
        {
	        $myinfo = I("session.my_info");
	        $data["aid"] = $myinfo["id"];
	        $data["published"]=date("Y-m-d H:i:s");
        	if ($news->add($data))
        	{
	           $this->success('成功提交');
		    } else {
		        $this->error("提交失败");
		    }
        }
       // $data['aid'] = $_SESSION['my_info']['aid'];
       

    }

    public function Delete()
    {
        $news = M('News');
        $id = $_GET['id'];
        $count = $news->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function Categories()
    {
    	  $M=M("Category");
    	  $this->list=$M->select();
    	  $this->display();
    }
    
    public function addCategory()
    {
    	$category=M('Category');
    	$pid=$_GET["pid"];
    	
    	$pName=$category->field("cid,pid,name")->where("cid=$pid")->select();
        if($pName==null)
        {
        	$this->pName="顶级分类";
        }
        else 
        {
        	$this->pName=$pName[0]["name"];
        }
    	$info["pid"]=$pid;
    	$this->info=$info;
    	$this->action="add";
    	$this->display();
    }
    
    public function editCategory()
    {
    	$id=$_GET["id"];
    	$category=M('Category');
        $category_Item=$category->field("cid,pid,name")->where("cid=$id")->find();
        
        $this->info=$category_Item;  
        $pid=$category_Item["pid"];      
        $pName=$category->field("cid,pid,name")->where("cid=$pid")->find();
        if($pName==null)
        {
        	$pName="顶级分类";
        }
        $this->pName=$pName;
    	$this->action="Edit";
    	$this->display(); 
    }
    
    public function saveCategory()
    {
    	$category=D("Category");
        $data = $category->create();
        if (!$data) $this->error($category->getError());
        if($_POST['action']=="Edit")
        {
        	 $category->save($data); // 根据条件保存修改的数据
        }
        else 
        {
        	 $category->add($data);
        }
        $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script><script type="text/javascript">$(function(){top.Dialog.alert("成功提交",function(){window.parent.frmright.location.reload();top.Dialog.close()})})</script>', 'utf-8');
    	
    }

    public function DeleteCategory()
    {
        $category = M('Category');
        $id = $_GET['cid'];
        $count = $category->delete($id);
        if ($count > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
}
?>